package top.superflow.version.db.jpa

import org.apache.commons.lang3.StringUtils
import top.superflow.exception.RequestUnsupportedException
import top.superflow.verify.Ensure
import top.superflow.version.compare.suggestion.Suggestion
import top.superflow.version.db.FieldPropertyVersion

class JpaFieldPropertyVersion (propName : String, propValue : Object)
                          extends FieldPropertyVersion
                          with Ensure{
  notEmpty(propName, "propName should not be null")
  notNull(propValue)
  
  this.name.compareAndSet(this.name.get(), propName)
  this.value.compareAndSet(this.value.get, propValue)
  
  def getId : String = {
    this.name.get
  }
  
  def compare(targetStructure: Any): Option[Suggestion] = ???
  def getMatchedChild(childDataVersionKey: Any): Option[top.superflow.version.db.Version] = ???
  
  def getPublicName = {
    JpaFieldPropertyVersion.getPublicName(this.name.get())
  }
}

object JpaFieldPropertyVersion extends Ensure{
  def getPublicName(originalName : String) : String = {
    notEmpty(originalName, "originalName should not be null")
    
    originalName match {
      case x if(StringUtils.equals(x, FieldPropertyVersion.JPA_FIELD_COLUMN_NAME)) => {
        FieldPropertyVersion.FIELD_COLUMN_NAME
      }
      
      case x if(StringUtils.equals(x, FieldPropertyVersion.JPA_FIELD_INSERTABLE)) => {
        FieldPropertyVersion.FIELD_INSERTABLE
      }
      
      case x if(StringUtils.equals(x, FieldPropertyVersion.JPA_FIELD_LENGTH)) => {
        FieldPropertyVersion.FIELD_LENGTH
      }
      
      case x if(StringUtils.equals(x, FieldPropertyVersion.JPA_FIELD_NULLABLE)) => {
        FieldPropertyVersion.FIELD_NULLABLE
      }
      
      case x if(StringUtils.equals(x, FieldPropertyVersion.JPA_FIELD_PRECISION)) => {
        FieldPropertyVersion.FIELD_PRECISION
      }
      
      case x if(StringUtils.equals(x, FieldPropertyVersion.JPA_FIELD_SCALE)) => {
        FieldPropertyVersion.FIELD_SCALE
      }
      
      case x if(StringUtils.equals(x, FieldPropertyVersion.JPA_FIELD_UNIQUE)) => {
        FieldPropertyVersion.FIELD_UNIQUE
      }
      
      case x if(StringUtils.equals(x, FieldPropertyVersion.JPA_FIELD_UPDATABLE)) => {
        FieldPropertyVersion.FIELD_UPDATABLE
      }

      case x if(StringUtils.equals(x, FieldPropertyVersion.JPA_FIELD_TYPE)) => {
        FieldPropertyVersion.FIELD_TYPE
      }

      case _ => {
        throw new RequestUnsupportedException("originalName " + originalName + " is not supported")
      }
    }
  }
}